/*
 *  This file is part of AtlasMapper server and clients.
 *
 *  Copyright (C) 2012 Australian Institute of Marine Science
 *
 *  Contact: Gael Lafond <g.lafond@aims.org.au>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package au.gov.aims.metadatagenerator.entity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Project extends Entity {
	private static final Logger LOGGER = Logger.getLogger(Project.class.getName());
	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	private static final String GEONETWORK_URL = "http://e-atlas.org.au/geonetwork";

	private String projectNumber;
	private String url;
	private boolean completed = false;
	private String uuid;
	private String title;
	private String _abstract;
	private String purpose;

	// Temporal extent
	// To add a temporal extent using GeoNetwork interface:
	//     Extent [+]
	//         Temporal element [+] [Spatial Temporal Extent (gmd:EX_SpatialTemporalExtent]
	//             Extent [+] [Time period (gml:TimePeriod)]
	//                 Choice [+] [Begin date (gml:beginPosition)]
	//                     Begin date [...]
	//                 Choice [+] [End date (gml:endPosition)]
	//                     End date [...]

	private Date start;
	private Date end;

	// Spacial extent
	private String studyLocations;

	private String suggestedSubmissionMethod;
	private List<Contact> pointsOfContact;

	private String topicCategoryCode;

	private String credit;

	private List<String> wkts;

	private String wmsLayerPoints;
	private String wmsLayerLines;
	private String wmsLayerPolygons;
	private String pdfMapURL;

	private String parentUUID;

	public String getProjectNumber() {
		return this.projectNumber;
	}
	public void setProjectNumber(String projectNumber) {
		this.projectNumber = projectNumber;
	}

	public String getURL() {
		return this.url;
	}
	public void setURL(String url) {
		this.url = url;
	}

	public boolean isCompleted() {
		return this.completed;
	}
	public void setCompleted(boolean completed) {
		this.completed = completed;
	}

	public String getUUID() {
		return this.uuid;
	}
	public void setUUID(String uuid) {
		this.uuid = uuid;
	}

	public String getTitle() {
		return this.title;
	}
	public void setTitle(String title) {
		this.title = title;
	}

	public String getAbstract() {
		return this._abstract;
	}
	public void setAbstract(String _abstract) {
		this._abstract = _abstract;
	}

	public String getPurpose() {
		return this.purpose;
	}
	public void setPurpose(String purpose) {
		this.purpose = purpose;
	}

	public Date getStart() {
		return this.start;
	}
	public String getFormattedStart() {
		return formatDate(this.start);
	}
	public void setStart(String startStr) {
		this.setStart(parseDate(startStr));
	}
	public void setStart(Date start) {
		this.start = start;
	}

	public Date getEnd() {
		return this.end;
	}
	public String getFormattedEnd() {
		return formatDate(this.end);
	}
	public void setEnd(String endStr) {
		this.setEnd(parseDate(endStr));
	}
	public void setEnd(Date end) {
		this.end = end;
	}

	public String getStudyLocations() {
		return this.studyLocations;
	}
	public void setStudyLocations(String studyLocations) {
		this.studyLocations = studyLocations;
	}

	public String getSuggestedSubmissionMethod() {
		return this.suggestedSubmissionMethod;
	}
	public void setSuggestedSubmissionMethod(String suggestedSubmissionMethod) {
		this.suggestedSubmissionMethod = suggestedSubmissionMethod;
	}

	public List<Contact> getPointsOfContact() {
		return this.pointsOfContact;
	}
	public void setPointsOfContact(List<Contact> pointsOfContact) {
		this.pointsOfContact = pointsOfContact;
	}
	public void addPointOfContact(Contact pointOfContact) {
		if (pointOfContact != null) {
			if (this.pointsOfContact == null) {
				this.pointsOfContact = new ArrayList<Contact>();
			}
			this.pointsOfContact.add(pointOfContact);
		}
	}

	public String getTopicCategoryCode() {
		return this.topicCategoryCode;
	}
	public void setTopicCategoryCode(String topicCategoryCode) {
		this.topicCategoryCode = topicCategoryCode;
	}

	public String getCredit() {
		return this.credit;
	}
	public void setCredit(String credit) {
		this.credit = credit;
	}

	public List<String> getWKTs() {
		return this.wkts;
	}
	public void setWKTs(List<String> wkts) {
		this.wkts = wkts;
	}
	public void addWKT(String wkt) {
		if (this.wkts == null) {
			this.wkts = new ArrayList<String>();
		}
		this.wkts.add(wkt);
	}

	public String getWMSLayerPoints() {
		return this.wmsLayerPoints;
	}
	public void setWMSLayerPoints(String wmsLayer) {
		this.wmsLayerPoints = wmsLayer;
	}
	public String getWMSLayerLines() {
		return this.wmsLayerLines;
	}
	public void setWMSLayerLines(String wmsLayer) {
		this.wmsLayerLines = wmsLayer;
	}
	public String getWMSLayerPolygons() {
		return this.wmsLayerPolygons;
	}
	public void setWMSLayerPolygons(String wmsLayer) {
		this.wmsLayerPolygons = wmsLayer;
	}

	public String getPDFMapURL() {
		return this.pdfMapURL;
	}
	public void setPDFMapURL(String pdfMapURL) {
		this.pdfMapURL = pdfMapURL;
	}

	public String getParentUUID() {
		return this.parentUUID;
	}
	public void setParentUUID(String parentUUID) {
		this.parentUUID = parentUUID;
	}


	public String getPointOfTruthURL() {
		return GEONETWORK_URL + "/srv/en/metadata.show?uuid=" + this.getUUID();
	}


	private static Date parseDate(String dateStr) {
		try {
			return DATE_FORMAT.parse(dateStr);
		} catch(Exception ex) {
			LOGGER.log(Level.WARNING, "Can not parse the date [" + dateStr + "]", ex);
		}
		return null;
	}
	private static String formatDate(Date date) {
		if (date == null) {
			return null;
		}
		return DATE_FORMAT.format(date);
	}
}
